package com.example.hotel_lxy.adapter;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.bean.Cell;
import com.example.hotel_lxy.bean.ColTitle;
import com.example.hotel_lxy.bean.DataInfoBean;
import com.example.hotel_lxy.bean.RowTitle;
import com.example.hotel_lxy.bean.WHBean;
import com.example.hotel_lxy.ui.activity.MainActivity;
import com.example.hotel_lxy.ui.activity.ReserveActivity;
import com.example.hotel_lxy.utils.LunarHelper;
import com.example.hotel_lxy.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import cn.zhouchaoyuan.excelpanel.BaseExcelPanelAdapter;


/**
 * Created by zhouchaoyuan on 2017/1/14.
 */

public class CustomAdapter extends BaseExcelPanelAdapter<RowTitle, ColTitle, Cell> {

    private Context context;
    private View.OnClickListener blockListener;
    private View.OnClickListener blockListenernoon;
    private List<List<DataInfoBean.DataBean.ListBean>> mlist;
    List<WHBean> whList;
    private int type;

    public CustomAdapter(Context context, View.OnClickListener blockListener, List<List<DataInfoBean.DataBean.ListBean>> mlist, View.OnClickListener blockListenernoon, int type) {
        super(context);
        this.context = context;
        this.blockListener = blockListener;
        this.mlist = mlist;
        this.blockListenernoon = blockListenernoon;
        this.type = type;
    }

    //=========================================content's cell===========================================
    @Override
    public RecyclerView.ViewHolder onCreateCellViewHolder(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.room_status_normal_cell, parent, false);
        CellHolder cellHolder = new CellHolder(layout);
        return cellHolder;
    }

    @Override
    public void onBindCellViewHolder(RecyclerView.ViewHolder holder, int verticalPosition, int horizontalPosition) {
        Cell cell = getMajorItem(verticalPosition, horizontalPosition);
        if (null == holder || !(holder instanceof CellHolder) || cell == null) {
            return;
        }
        CellHolder viewHolder = (CellHolder) holder;
        ColTitle leftItem = getLeftItem(verticalPosition);
        RowTitle rowTitle = getTopItem(horizontalPosition);
       // viewHolder.title_data.setText(rowTitle.getDateString() + "");
      //  viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_E5E5E5));
       // viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_E5E5E5));
//        viewHolder.bookingId.setText("NULL");
//        viewHolder.channelId.setText("NULL");
        String string1 = SPUtils.get(context, "homeorder", "").toString();
        String data1 = "";
        String begin_night = "";
        String sched_id = "";
        if (!string1.equals("")) {
            String[] split1 = string1.split("-");
            data1 = split1[0] + "-" + split1[1] + "-" + split1[2];
            begin_night = split1[3];
            sched_id = split1[4];
        }
        for (int iq = 0; iq < mlist.size(); iq++) {
            List<DataInfoBean.DataBean.ListBean> listBeans = mlist.get(iq);
            for (int i = 0; i < listBeans.size(); i++) {
                DataInfoBean.DataBean.ListBean listBean = listBeans.get(i);
                if (listBean.getOrder() != null) {
                    if (leftItem.getId().equals(listBean.getId() + "")) {

                        viewHolder.title_name.setText(leftItem.getId() + "");
                        for (int j = 0; j < listBean.getOrder().size(); j++) {
                            DataInfoBean.DataBean.ListBean.OrderBean orderBean = listBean.getOrder().get(j);
                            if (rowTitle.getDateString().equals(orderBean.getDay() + "")) {
//                                if (leftItem.getId().equals("4")) {
//                                    if (orderBean.getDay().equals("2021-06-19")) {
//                                        Log.e("TAG", orderBean.getDay() + " ");
//                                    }
//                                }
                                String[] split = rowTitle.getDateString().split("-");
                                if (split[2].equals("01")) {
                                    if (split[1].equals("10")) {
                                        viewHolder.bookingName.setText(split[1] + "月");
                                    } else {
                                        viewHolder.bookingName.setText(split[1].replace("0", "") + "月");
                                    }
                                    viewHolder.bookingName.setTextColor(context.getResources().getColor(R.color.color_FFCA00));
                                } else {
                                    viewHolder.bookingName.setText(split[2]);
                                }
                                viewHolder.text.setText(split[0] + "-" + split[1] + "-" + split[2]);
                                String data = split[0] + "-" + split[1] + "-" + split[2];
                                if (orderBean.getIs_meeting() == 1) {//1会议2宴会
                                    if (orderBean.getBegin_night() == 1) {
//                                        viewHolder.bookingId.setText(orderBean.getId() + "");
                                        if (orderBean.getType() == 2) {//1潜客2预定
                                            if (data.equals("" + data1)) {
                                                if (begin_night.equals("" + orderBean.getBegin_night())) {
                                                    if (sched_id.equals("" + leftItem.getId())) {
                                             //           viewHolder.cellContainer.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.line_bg_15d37d));
                                                    }else {
                                              //          viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_15D37D));
                                                    }
                                                } else {
                                           //         viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_15D37D));
                                                }
                                            }
                                            else {
                                           //     viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_15D37D));
                                            }
                                        } else {
                                            if (data.equals("" + data1)) {
                                                if (begin_night.equals("" + orderBean.getBegin_night())) {
                                                    if (sched_id.equals("" + leftItem.getId())) {
                                                //        viewHolder.cellContainer.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.line_bg_ffca00));
                                                    }else {
                                              //          viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_FFCA00));
                                                    }
                                                } else {
                                          //          viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_FFCA00));
                                                }
                                            } else {
                                          //      viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_FFCA00));
                                            }
                                        }
                                    } else {
//                                        viewHolder.channelId.setText(orderBean.getId() + "");
                                        if (orderBean.getType() == 2) {
                                            if (data.equals("" + data1)) {
                                                if (begin_night.equals("" + orderBean.getBegin_night())) {
                                                    if (sched_id.equals("" + leftItem.getId())) {
                                             //           viewHolder.Container.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.line_bg_15d37d));
                                                    }else {
                                            //            viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_15D37D));
                                                    }
                                                } else {
                                            //        viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_15D37D));
                                                }
                                            } else {
                                          //      viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_15D37D));
                                            }
                                        } else {
                                            if (data.equals("" + data1)) {
                                                if (begin_night.equals("" + orderBean.getBegin_night())) {
                                                    if (sched_id.equals("" + leftItem.getId())) {
                                               //         viewHolder.Container.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.line_bg_ffca00));
                                                    }else {
                                           //             viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_FFCA00));
                                                    }
                                                } else {
                                         //           viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_FFCA00));
                                                }
                                            } else {
                                         //       viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_FFCA00));
                                            }
                                        }
                                    }

                                }
                                else {
                                    if (orderBean.getBegin_night() == 1) {
//                                        viewHolder.bookingId.setText(orderBean.getId() + "");
                                        if (orderBean.getType() == 2) {//1潜客2预定
                                            if (data.equals("" + data1)) {
                                                if (begin_night.equals("" + orderBean.getBegin_night())) {
                                                    if (sched_id.equals("" + leftItem.getId())) {
                                                  //      viewHolder.cellContainer.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.line_bg_15d37d));
                                                    }else {
                                                  //      viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_15D37D));
                                                    }
                                                } else {
                                             //       viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_15D37D));
                                                }
                                            } else {
                                             //   viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_15D37D));
                                            }
                                        } else {
                                            if (data.equals("" + data1)) {
                                                if (begin_night.equals("" + orderBean.getBegin_night())) {
                                                    if (sched_id.equals("" + leftItem.getId())) {
                                                 //       viewHolder.cellContainer.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.line_bg_ffca00));
                                                    }else {
                                                  //      viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_FFCA00));
                                                    }
                                                } else {
                                                //    viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_FFCA00));
                                                }
                                            } else {
                                               // viewHolder.cellContainer.setBackgroundColor(context.getResources().getColor(R.color.color_FFCA00));
                                            }
                                        }
                                    } else {
//                                        viewHolder.channelId.setText(orderBean.getId() + "");
                                        if (orderBean.getType() == 2) {
                                            if (data.equals("" + data1)) {
                                                if (begin_night.equals("" + orderBean.getBegin_night())) {
                                                    if (sched_id.equals("" + leftItem.getId())) {
                                                 //       viewHolder.Container.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.line_bg_15d37d));
                                                    }else {
                                                 //       viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_15D37D));
                                                    }
                                                } else {
                                              //      viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_15D37D));
                                                }
                                            } else {
                                              //  viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_15D37D));
                                            }
                                        } else {
                                            if (data.equals("" + data1)) {
                                                if (begin_night.equals("" + orderBean.getBegin_night())) {
                                                    if (sched_id.equals("" + leftItem.getId())) {
                                                  //      viewHolder.Container.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.line_bg_ffca00));
                                                    }else{
                                                //        viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_FFCA00));
                                                    }
                                                } else {
                                             //       viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_FFCA00));
                                                }
                                            } else {
                                         //       viewHolder.Container.setBackgroundColor(context.getResources().getColor(R.color.color_FFCA00));
                                            }
                                        }
                                    }
                                }
                                //设置标题的格式
                                String string = LunarHelper.SolarToLunarString(Integer.valueOf(split[0]), Integer.valueOf(split[1]), Integer.valueOf(split[2]));
                                if (LunarHelper.Md(string)) {
                                    viewHolder.channelName.setTextColor(context.getResources().getColor(R.color.color_FA0101));
                                } else {
                                    viewHolder.channelName.setTextColor(context.getResources().getColor(R.color.color_333333));
                                }
                                viewHolder.channelName.setText(string);
                            } else {
                                viewHolder.title_name.setText(leftItem.getId() + "");
                                String[] split = rowTitle.getDateString().split("-");
                                if (split[2].equals("01")) {
                                    if (split[1].equals("10")) {
                                        viewHolder.bookingName.setText(split[1] + "月");
                                    } else {
                                        viewHolder.bookingName.setText(split[1].replace("0", "") + "月");
                                    }
                                    viewHolder.bookingName.setTextColor(context.getResources().getColor(R.color.color_FA0101));
                                } else {
                                    viewHolder.bookingName.setText(split[2]);
                                    viewHolder.text.setText(split[0] + "-" + split[1] + "-" + split[2]);
                                }
                                //设置标题的格式
                                String string = LunarHelper.SolarToLunarString(Integer.valueOf(split[0]), Integer.valueOf(split[1]), Integer.valueOf(split[2]));
                                if (LunarHelper.Md(string)) {
                                    viewHolder.channelName.setTextColor(context.getResources().getColor(R.color.color_FA0101));
                                } else {
                                    viewHolder.channelName.setTextColor(context.getResources().getColor(R.color.color_333333));
                                }
                                viewHolder.channelName.setText(string);
                            }
                        }
                    } else {
                        viewHolder.title_name.setText(leftItem.getId() + "");
                        String[] split = rowTitle.getDateString().split("-");
                        if (split[2].equals("01")) {
                            if (split[1].equals("10")) {
                                viewHolder.bookingName.setText(split[1] + "月");
                            } else {
                                viewHolder.bookingName.setText(split[1].replace("0", "") + "月");
                            }
                            viewHolder.bookingName.setTextColor(context.getResources().getColor(R.color.color_FA0101));
                        } else {
                            viewHolder.bookingName.setText(split[2]);
                            viewHolder.text.setText(split[0] + "-" + split[1] + "-" + split[2]);
                        }
                        //设置标题的格式
                        String string = LunarHelper.SolarToLunarString(Integer.valueOf(split[0]), Integer.valueOf(split[1]), Integer.valueOf(split[2]));
                        if (LunarHelper.Md(string)) {
                            viewHolder.channelName.setTextColor(context.getResources().getColor(R.color.color_FA0101));
                        } else {
                            viewHolder.channelName.setTextColor(context.getResources().getColor(R.color.color_333333));
                        }
                        viewHolder.channelName.setText(string);
                    }
                } else {
                    viewHolder.title_name.setText(leftItem.getId() + "");
                    String[] split = rowTitle.getDateString().split("-");
                    if (split[2].equals("01")) {
                        if (split[1].equals("10")) {
                            viewHolder.bookingName.setText(split[1] + "月");
                        } else {
                            viewHolder.bookingName.setText(split[1].replace("0", "") + "月");
                        }
                        viewHolder.bookingName.setTextColor(context.getResources().getColor(R.color.color_FA0101));
                    } else {
                        viewHolder.bookingName.setText(split[2]);
                        viewHolder.text.setText(split[0] + "-" + split[1] + "-" + split[2]);
                    }
                    //设置标题的格式
                    String string = LunarHelper.SolarToLunarString(Integer.valueOf(split[0]), Integer.valueOf(split[1]), Integer.valueOf(split[2]));
                    if (LunarHelper.Md(string)) {
                        viewHolder.channelName.setTextColor(context.getResources().getColor(R.color.color_FA0101));
                    } else {
                        viewHolder.channelName.setTextColor(context.getResources().getColor(R.color.color_333333));
                    }
                    viewHolder.channelName.setText(string);
                }
            }
        }
        if (mlist.size() != 0) {
            int p = verticalPosition + 1;
            if (p == mlist.get(0).size()) {
                viewHolder.blow.setVisibility(View.GONE);
            }
        }
//        viewHolder.cellContainer.setTag(viewHolder.bookingId.getText().toString() + "/" + viewHolder.title_data.getText().toString() + "/" + viewHolder.title_name.getText().toString());
//        viewHolder.cellContainer.setOnClickListener(blockListener);
//        viewHolder.Container.setTag(viewHolder.channelId.getText().toString() + "/" + viewHolder.title_data.getText().toString() + "/" + viewHolder.title_name.getText().toString());
//        viewHolder.Container.setOnClickListener(blockListenernoon);

        ViewGroup.LayoutParams lp = viewHolder.line2.getLayoutParams();


//        ViewGroup.LayoutParams lpcentercellContainer = viewHolder.cellContainer.getLayoutParams();
      //  ViewGroup.LayoutParams lpcenterContainer = viewHolder.Container.getLayoutParams();


        lp.width = whList.get(0).getContainerwidth();
        lp.height = whList.get(0).getContainerheight();
//        lpcentercellContainer.width = whList.get(0).getPmswidth();
//        lpcentercellContainer.height = whList.get(0).getPmsheight();
      //  lpcenterContainer.width = whList.get(0).getPmswidth();
     //   lpcenterContainer.height = whList.get(0).getPmsheight();
        viewHolder.line2.setLayoutParams(lp);
//        viewHolder.cellContainer.setLayoutParams(lpcentercellContainer);
      //  viewHolder.Container.setLayoutParams(lpcenterContainer);


    }

    static class CellHolder extends RecyclerView.ViewHolder {
        public final TextView bookingName;
        public final TextView channelName;
//        public final TextView bookingId;
//        public final TextView channelId;
//        public final LinearLayout cellContainer;
      //  public final LinearLayout Container;
        private final TextView text;
        public final LinearLayout line2;
        public final View center, blow;
        public final TextView title_name;
//        public final TextView title_data;

        public CellHolder(View itemView) {
            super(itemView);
            bookingName = itemView.findViewById(R.id.booking_name);
            channelName = itemView.findViewById(R.id.channel_name);
//            bookingId = itemView.findViewById(R.id.booking_id);
//            channelId = itemView.findViewById(R.id.channel_id);
//            cellContainer = itemView.findViewById(R.id.pms_cell_container);//上
          //  Container = itemView.findViewById(R.id.pms_container);//下
            text = itemView.findViewById(R.id.text);
            line2 = itemView.findViewById(R.id.line2);
            center = itemView.findViewById(R.id.center);
            blow = itemView.findViewById(R.id.blow);
            title_name = itemView.findViewById(R.id.title_name);
//            title_data = itemView.findViewById(R.id.title_data);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateTopViewHolder(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.room_status_top_header_item, parent, false);
        TopHolder topHolder = new TopHolder(layout);
        return topHolder;
    }

    @Override
    public void onBindTopViewHolder(RecyclerView.ViewHolder holder, int position) {
        RowTitle rowTitle = getTopItem(position);

        if (null == holder || !(holder instanceof TopHolder) || rowTitle == null) {
            return;
        }
        TopHolder viewHolder = (TopHolder) holder;
        viewHolder.roomWeek.setText(rowTitle.getWeekString().replace("星期", "周"));

        ViewGroup.LayoutParams lp = viewHolder.line1.getLayoutParams();
        ViewGroup.LayoutParams lpWeek = viewHolder.roomWeek.getLayoutParams();
        if (whList.get(0).getWeekSize() > 12) {
            viewHolder.roomWeek.setTextSize(12);
        } else if (whList.get(0).getWeekSize() < 8) {
            viewHolder.roomWeek.setTextSize(8);
        } else {
            viewHolder.roomWeek.setTextSize(whList.get(0).getWeekSize());
        }

        lp.width = whList.get(0).getWeekWidth();
        lp.height = whList.get(0).getWeekHeight();

        lpWeek.width = whList.get(0).getWeekWidth();
        lpWeek.height = whList.get(0).getWeekHeight();


        viewHolder.line1.setLayoutParams(lp);
        viewHolder.roomWeek.setLayoutParams(lpWeek);

    }

    static class TopHolder extends RecyclerView.ViewHolder {
        public final TextView roomWeek;
        public final LinearLayout line1;

        public TopHolder(View itemView) {
            super(itemView);
            roomWeek = itemView.findViewById(R.id.week_label);
            line1 = itemView.findViewById(R.id.line1);
        }
    }

    //=========================================left cell===========================================
    @Override
    public RecyclerView.ViewHolder onCreateLeftViewHolder(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.room_status_left_header_item, parent, false);
        LeftHolder leftHolder = new LeftHolder(layout);
        return leftHolder;
    }

    @Override
    public void onBindLeftViewHolder(RecyclerView.ViewHolder holder, int position) {
        ColTitle colTitle = getLeftItem(position);
        if (null == holder || !(holder instanceof LeftHolder) || colTitle == null) {
            return;
        }
        LeftHolder viewHolder = (LeftHolder) holder;
        viewHolder.roomNumberLabel.setText(colTitle.getRoomNumber());
        ViewGroup.LayoutParams lp = viewHolder.root.getLayoutParams();
        ViewGroup.LayoutParams lpLabel = viewHolder.roomNumberLabel.getLayoutParams();
        ViewGroup.LayoutParams lpline = viewHolder.left_item_container.getLayoutParams();
        ViewGroup.LayoutParams blow = viewHolder.blow.getLayoutParams();

        int p = position + 1;
        if (mlist.size() != 0) {
            if (p == mlist.get(0).size()) {
                viewHolder.blow.setVisibility(View.GONE);
            }
        } else {
            return;
        }

        viewHolder.roomNumberLabel.setTextSize(whList.get(0).getRootSize());

        lp.width = whList.get(0).getRootwidth();
        lp.height = whList.get(0).getRootheight() - 3;
        lpline.width = whList.get(0).getRootwidth();
        lpline.height = whList.get(0).getRootheight() - 3;
        lpLabel.width = whList.get(0).getRootwidth();
        lpLabel.height = whList.get(0).getRootheight() - 3;
        blow.width = whList.get(0).getRootwidth();
//        blow.height=whList.get(0).getBlow();

        viewHolder.roomNumberLabel.setLayoutParams(lpLabel);
        viewHolder.left_item_container.setLayoutParams(lpLabel);
        viewHolder.blow.setLayoutParams(blow);
        viewHolder.root.setLayoutParams(lp);
    }

    static class LeftHolder extends RecyclerView.ViewHolder {

        public final TextView roomNumberLabel;
        public final LinearLayout root;
        public final LinearLayout left_item_container;
        public final View blow;

        public LeftHolder(View itemView) {
            super(itemView);
            root = itemView.findViewById(R.id.root);
            roomNumberLabel = itemView.findViewById(R.id.room_number_label);
            left_item_container = itemView.findViewById(R.id.left_item_container);
            blow = itemView.findViewById(R.id.blow);
        }
    }

    //=========================================left-top cell===========================================
    @Override
    public View onCreateTopLeftView() {
        return LayoutInflater.from(context).inflate(R.layout.room_status_normal, null);
    }

    public void setSf(List<WHBean> whList, int type) {
        this.whList = whList;
        this.type = type;
        notifyDataSetChanged();
    }
}
